package com.tianruan.dc.modules.common.api;

import com.tianruan.dc.common.utils.OSSUtils;
import com.tianruan.dc.common.utils.response.VResponse;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.Console;
import java.util.Calendar;

/**
 * @PS
 */
@RestController
@RequestMapping("/api/common/upload")
public class uploadController {

    @PostMapping("/img")
    public VResponse<Object> uploadImg(@RequestParam("img") MultipartFile img) {
        Calendar calendar = Calendar.getInstance();
        String s = OSSUtils.uploadFileInputSteam("/img/" + calendar.get(Calendar.YEAR) + "/" + (calendar.get(Calendar.MONTH) + 1) + "/" + calendar.get(Calendar.DAY_OF_MONTH) + "/", img);
        return VResponse.success(s);
    }

    @PostMapping("/file")
    public VResponse<Object> uploadFile(@RequestParam("file") MultipartFile file) {

//        // todo 判断文件名是否存在特殊字符
//
        //获取文件名
        String originalFilename = file.getOriginalFilename();
        //获取最后一个"."的位置
        int cutPoint = originalFilename.lastIndexOf(".");
        //获取文件名
        String prefix = originalFilename.substring(0, cutPoint);
        //获取后缀名
        String suffix = originalFilename.substring(cutPoint + 1);

        if (prefix.length() > 50) {
            return VResponse.error(1000, "上传的文件名称过长");
        }

        Calendar calendar = Calendar.getInstance();
        String s = OSSUtils.uploadFileInputSteam("/file/" + calendar.get(Calendar.YEAR) + "/" + (calendar.get(Calendar.MONTH) + 1) + "/" + calendar.get(Calendar.DAY_OF_MONTH) + "/", file);
        return VResponse.success(s);
    }

}
